<?php

require_once 'db_connect.php';
require_once 'Slim/Slim.php';
/*
 * Creating new Slim application
 */
$app = new Slim();
$db = new DB_CONNECT();
/*
 * Get topplace (depending on whether ID was provided)
 */

$app->get('/place(/:id)', function ($id = null) use ($app) {
    $data = array();
    if ($id === null) { 
		
    } else {
        $data = array();
		$query = 'SELECT * FROM place, district WHERE place.place_id = '.$id.' and place.district_id= district.id';
		$result = mysql_query($query) or die(mysql_error());
		if (mysql_num_rows($result) > 0) {			
                while ($row = mysql_fetch_array($result)) {
					
					$data[] = array(
					'place_id' =>			$row['place_id'],
					'place_name' =>     	$row['place_name'],
					'address' =>       		$row['address'],
					'district_name'	=>		$row['name'],
					'longitude'		=>		$row['longitude'],
					'latitude'		=>		$row['latitude'],
					'phone_number'		=>		$row['phone_number'],
					'fax_number'		=>		$row['fax_number'],
					'email'				=>		$row['email'],
					'website'		=>		$row['website'],
					'open_time'		=>		$row['open_time'],
					'close_time'		=>		$row['close_time'],
					'lowest_price'		=>		$row['lowest_price'],
					'hightest_price'		=>		$row['hightest_price'],
					'description'		=>		$row['description'],
					'url_avatar' =>			$row['url_avatar'],
					'name_avatar' =>		$row['name_avatar'],
					'date_create' =>			$row['date_create']
				);
                }
        } 
    }
    $app->response()->header('Content-Type', 'application/json');
    echo json_encode($data);
});

/*
 * Runing the Slim app
 */
$app->run();